package com.pucong.gitee.database.backup.business.sys.controller;

import com.pucong.gitee.database.backup.business.sys.service.GetLogFileContentService;
import com.pucong.gitee.database.backup.common.entity.sys.vo.LogFileListVo;
import com.pucong.gitee.database.backup.common.entity.sys.vo.WebSocketReturnVo;
import com.pucong.gitee.database.backup.utils.Res;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author pucong
 */
@RestController
@RequestMapping("/system/logFile")
@Api(tags = "系统日志文件获取 -API")
public class SystemLogFileController {

    @Autowired
    private GetLogFileContentService getLogFileContentService;

    @GetMapping("/getLogFileList")
    @ApiOperation(value = "1 获取日志文件list")
    public Res<List<String>> getLogFileList(){
        List<String> fileNameList = getLogFileContentService.getLogFileNameList();
        return Res.ok(fileNameList);
    }

    @GetMapping("/getLogFileContent")
    @ApiOperation(value = "2 获取日志文件内容")
    public Res<WebSocketReturnVo> getLogFileContent(LogFileListVo logFileListVo){
        WebSocketReturnVo webSocketReturnVo = getLogFileContentService.getLogFile(logFileListVo.getLogFileName(),logFileListVo.getStartLine(),logFileListVo.getMaxLine());
        return Res.ok(webSocketReturnVo);
    }

    @GetMapping("/downloadLogFile")
    @ApiOperation(value = "3 下载日志文件")
    public void downloadLogFile(HttpServletResponse response, String fileName) throws IOException {
        getLogFileContentService.downloadLogFile(response, fileName);
    }

    @DeleteMapping("/getLogFileContent")
    @ApiOperation(value = "4 删除过期日志文件")
    public Res getLogFileContent(){
        getLogFileContentService.deleteLogFile();
        return Res.ok();
    }
}
